package com.jinghao.core.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.jinghao.core.common.exception.JingHaoBaseException;
import com.jinghao.core.common.exception.JingHaoCoreException;
import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.util.JhLoggerUtil;
import com.jinghao.core.common.view.JingHaoBaseView;   

/** 
  * @Description:
  * @author  申湘岭 
  * @date 创建时间：2016年7月3日 上午12:30:15 
  * @version 1.0 
*/
public class JingHaoExceptionController implements  HandlerExceptionResolver {
	private final Logger logger = JhLoggerUtil.getLogger();
    private JingHaoBaseView view=null;
	@Override
	public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
			Exception arg3) {
		// 根据不同错误转向不同页面   
		view=new JingHaoBaseView();
		view.getBaseModel().setIsError();
        if(arg3 instanceof JingHaoBaseException) {  
        	//view.setView(new MappingJackson2JsonView());
        	/* Map map=new HashMap();   
             map.put("1111", "22222");
             map.put("1111", "22222");
             map.put("22", "22222");
             return new ModelAndView(new MappingJackson2JsonView(),map);   */
        	view.getBaseModel().setMssage(arg3.getMessage());
         }else if(arg3 instanceof JingHaoCustomerException) { 
        	 JingHaoCustomerException arg=(JingHaoCustomerException)arg3;
        	 view.getBaseModel().setMssage(arg.getMsgDes());
        	 view.getBaseModel().setMsgCode(arg.getRetCd());
         } else if(arg3 instanceof JingHaoCoreException){ 
        	 JingHaoCoreException arg=(JingHaoCoreException)arg3;
        	 view.getBaseModel().setMssage(arg.getMsgDes());
        	 view.getBaseModel().setMsgCode(arg.getRetCd());
         }else{
        	  Exception e =(Exception)arg3;
        	  e.printStackTrace();
        	 this.logger.info("系统错误："+arg3.getMessage());
        	 view.getBaseModel().setMssage("操作失败，请联系系统管理员！");
         }
        view.getModelMap().addAllAttributes(view.getJson());
        this.setBackView(view);
        return view;
	}
	/**
	 * 该方法进行扩展数据返回类型，可进行支持返回json,freemark,jsp等
	 * @param view
	 */
	private void setBackView(JingHaoBaseView view){
		if(view.getResultType().equals("json")){
			view.setView(new MappingJackson2JsonView());
		}else if(view.getResultType().equals("freemark")){
			
		}else{
			
		}
	}
	
	 /** 基于@ExceptionHandler异常处理 */  
    /*@ExceptionHandler  
    public String exp(HttpServletRequest request,@ModelAttribute("model") JingHaoBaseEntity model, Exception ex) {  
    	HttpServletRequest ccc=request;
        request.setAttribute("ex", ex);  
        System.out.println("pagestart:"+model.getPage().getStart());
        logger.debug("XMLHttpRequest:"+request.getHeader("X-Requested-With"));
       /*if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){
        	
        }*/
        /*
        if(ex instanceof JingHaoBaseException) {  
            return "business_error";  
        }else if(ex instanceof JingHaoCustomerException) {  
        	JingHaoCustomerException abc=(JingHaoCustomerException)ex;
        	logger.debug("info:"+abc.getRetCd());
        	logger.info("info:"+abc.getMsgDes());
            return "parameter_error";  
        } else {  
            return "error";  
        }  
    } */ 
}

 
